//
//  StepTwoViewController.m
//  nonghubao
//
//  Created by Fenrir on 13-8-17.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "StepTwoViewController.h"
#import "Helper.h"
#import "Config.h"
#import "StepThreeViewController.h"
#import "UICustomTextField.h"
#import "JMFBase.h"

@interface StepTwoViewController ()
{
    UICustomTextField *_textField;
}
@end

@implementation StepTwoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = _L(@"vc_title_072");
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_012@2x" ofType:@"png"]]];
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 70.0f, 20.0f)];
    titleLabel.text = _L(@"title_83");
    titleLabel.font = _SYS_FONT_003;
    
    _textField = [[UICustomTextField alloc] initWithFrame:CGRectMake(10.0f, 11.0f, 300.0f, 45.0f)];
    _textField.background = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_010@2x" ofType:@"png"]];
    _textField.returnKeyType = UIReturnKeyDone;
    _textField.placeholder = _L(@"holder_35");
    _textField.delegate = self;
    _textField.leftView = titleLabel;
    _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _textField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_textField];
    
    UIButton *bottomButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, _SYS_FRAME_HEIGHT-94.0f, 300.0f, 44.0f)];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomButton setTitle:_L(@"btn_yes_30") forState:UIControlStateNormal];
    [bottomButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_btn_006_n@2x" ofType:@"png"]] forState:UIControlStateNormal];
    bottomButton.titleLabel.font = _SYS_FONT_007;
    [bottomButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)buttonEvent:(id)sender
{
    JSONRequest *request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ACCOUNT_TRANSFER_FORGOT_SECURITYPWD_SECOND];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_01");
    [request setValue:_textField.text forParameter:@"code"];
    [request asynchronous];
}

#pragma mark - JSONRequestDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        StepThreeViewController *stepThreeVC = [[StepThreeViewController alloc] init];
        [self.navigationController pushViewController:stepThreeVC animated:YES];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
    
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

@end
